package com.promise.jokerdream.ui.settings

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.clickable
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import com.promise.jokerdream.ui.components.SettingCard
import com.promise.jokerdream.ui.components.RoundCheckbox
import com.promise.jokerdream.ui.theme.AppSpacing
import com.promise.jokerdream.ui.theme.AppFontSizes
import com.promise.jokerdream.database.entity.ImmortalsGameConfigEntity
import com.promise.jokerdream.config.game.ImmortalType

/**
 * 仙武修真设置卡片
 */
@Composable
fun ImmortalsSettingCard(
    config: ImmortalsGameConfigEntity,
    onImmortalsChange: (Boolean) -> Unit,
    onImmortalTypeChange: (ImmortalType) -> Unit,
    modifier: Modifier = Modifier,
    onExecuteClick: (() -> Unit)? = null
) {
    SettingCard(
        title = "仙武修真",
        description = "",
        checked = config.enabledSetting,
        onCheckedChange = onImmortalsChange,
        modifier = modifier,
        onExecuteClick = onExecuteClick,
        content = {
            if (config.enabledSetting) {
                Column(
                    modifier = Modifier.fillMaxWidth()
                ) {
                    @OptIn(ExperimentalLayoutApi::class)
                    FlowRow(
                        modifier = Modifier
                            .fillMaxWidth()
                            .padding(vertical = AppSpacing.componentSmall),
                        horizontalArrangement = Arrangement.spacedBy(AppSpacing.componentMedium),
                        verticalArrangement = Arrangement.spacedBy(AppSpacing.componentMedium)
                    ) {
                        // 力量山
                        ImmortalTypeChip(
                            type = ImmortalType.STRENGTH,
                            isSelected = config.selectedImmortalType == "STRENGTH",
                            onClick = { onImmortalTypeChange(ImmortalType.STRENGTH) }
                        )
                        
                        // 敏捷山
                        ImmortalTypeChip(
                            type = ImmortalType.AGILITY,
                            isSelected = config.selectedImmortalType == "AGILITY",
                            onClick = { onImmortalTypeChange(ImmortalType.AGILITY) }
                        )
                        
                        // 速度山
                        ImmortalTypeChip(
                            type = ImmortalType.SPEED,
                            isSelected = config.selectedImmortalType == "SPEED",
                            onClick = { onImmortalTypeChange(ImmortalType.SPEED) }
                        )
                        
                        // 推荐
                        ImmortalTypeChip(
                            type = ImmortalType.RECOMMENDED,
                            isSelected = config.selectedImmortalType == "RECOMMENDED",
                            onClick = { onImmortalTypeChange(ImmortalType.RECOMMENDED) }
                        )
                    }
                }
            }
        }
    )
}

/**
 * 仙武类型芯片
 */
@Composable
private fun ImmortalTypeChip(
    type: ImmortalType,
    isSelected: Boolean,
    onClick: () -> Unit
) {
    Row(
        modifier = Modifier
            .clickable { onClick() }
            .padding(vertical = AppSpacing.componentSmall),
        verticalAlignment = Alignment.CenterVertically
    ) {
        RoundCheckbox(
            checked = isSelected,
            onCheckedChange = { onClick() }
        )
        Spacer(modifier = Modifier.width(AppSpacing.componentMedium))
        Text(
            text = type.displayName,
            fontSize = AppFontSizes.mediumBody,
            color = MaterialTheme.colorScheme.onSurface
        )
    }
}
